﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TreaShares.Backoffice.DataLayer;

namespace TreaShares.Backoffice.Administrator
{
    public partial class Report2Clients : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                TreaShares.Backoffice.Controlers.ReportsManager rm = new Controlers.ReportsManager();
                int reportId = int.Parse(Request.QueryString["ReportId"].ToString());
                List<Report2ClientsResult> report2Clients = rm.SelectReport2Clients(reportId);
                foreach (var r2c in report2Clients)
                {
                    ListItem li = new ListItem(r2c.Name);
                    li.Selected = r2c.IsSelected.Value;
                    li.Value = r2c.Id.ToString();
                    this.cbClients.Items.Add(li);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            TreaShares.Backoffice.Controlers.ReportsManager rm = new Controlers.ReportsManager();
            int reportId = int.Parse(Request.QueryString["ReportId"].ToString());
            rm.DeleteAllClientReports(reportId);
            foreach (ListItem item in this.cbClients.Items)
            {
                bool isSelected = item.Selected;
                if (isSelected)
                {
                    Guid clientId = new Guid(item.Value);
                    rm.InsertReport2Clients(clientId, reportId);
                }
            }
        }

    }
}